package org.boot.controller;

import jakarta.annotation.Resource;
import org.boot.dao.CityRepository;
import org.boot.dao.MyBean;
import org.boot.entity.City;
import org.boot.event.HelloApplicationEvent;
import org.boot.event.MyEventPublisher;
import org.boot.redis.RedisBean;
import org.boot.service.AccountService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class HelloController {

    @Resource
    AccountService accountService;
    @Resource
    MyEventPublisher publisher;
    @Resource
    MyBean myBean;
    @Resource
    CityRepository cityRepository;
    @Resource
    RedisBean redisBean;

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    @RequestMapping("/hi")
    String hi() {
        return accountService.getX("mcc");
    }

    @RequestMapping("/event")
    String event() {
        HelloApplicationEvent event = new HelloApplicationEvent(this, "HelloController_event");
        publisher.publishEvent(event);
        return "event00";
    }

    @RequestMapping("/db")
    String db() {
        myBean.doSomething();
        return "DB";
    }


    @RequestMapping("/city")
    String city(@RequestParam("id") Long id) {
        Optional<City> optional = cityRepository.findById(id);
        optional.ifPresent(System.out::println);
        return "city";
    }

    @RequestMapping("/redis")
    String redis() {
        return redisBean.getName("name");
    }
}
